<template>
  <div class="swiper-container">
    <!-- 添加图片的弹出框 -->
    <div class="upload">
      <el-dialog
      ref="upload"
        title="提示"
        :visible.sync="dialogVisibleOuter"
        width="30%"
        :before-close="handleClose">
        <span>
          <el-upload :disabled="flag"
            :action="getUploadUrl()"
            list-type="picture-card"
            :auto-upload="true"
            :on-success="handleSuccess"
            :limit='1'>
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{file}">
              <img
                class="el-upload-list__item-thumbnail"
                :src="file.url" alt=""
                style="width:146px;height:146px"
              >
              <span class="el-upload-list__item-actions">
                <span
                  class="el-upload-list__item-preview"
                  @click="handlePictureCardPreview(file)"
                >
                  <i class="el-icon-zoom-in"></i>
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleDownload(file)"
                >
                  <i class="el-icon-download"></i>
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleRemove(file)"
                >
                  <i class="el-icon-delete"></i>
                </span>
              </span>
            </div>
          </el-upload>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleOuter = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisibleOuter = false;addSwiper()">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog :visible.sync="dialogVisibleInner">
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
    </div>
    <!-- 添加按钮 -->
    <div class="add">
      <el-button @click="dialogVisibleOuter = true" type="success" style="margin-bottom: 10px">添加</el-button>
    </div>
    <!-- 展示数据的表格 -->
      <div class="table">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        height="750">
        <el-table-column
          prop="id"
          label="编号"
          type="index"
          width="150"
          align="center">
        </el-table-column>
        <el-table-column
          label="内容">
          <template slot-scope="scope">
            <img style="width:80px;height:80px" :src="scope.row.url" alt="">
          </template>
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row.id)" type="danger" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { getSwiperList,deleteSwiper,postSwiper } from "../../api/admin/swiper.js";
export default {
  data() {
    return {
      tableData:[],
      dialogImageUrl: '',
      dialogVisibleInner: false,
      dialogVisibleOuter: false,
      disabled: false,
      flag:false,
      imgurl:''
    };
  },
  methods: {
    init() {
      getSwiperList().then( (res) => {
        // console.log(res.data);
      this.tableData = res.data.data
      })
    },
    // 获取图片上传的请求路径
    getUploadUrl() {
      return process.env.VUE_APP_UPLOAD_API;
    },
    // 上传图片
    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisibleInner = true;
    },
    handleDownload(file) {
      console.log(file);
    },
    handleSuccess(response, file, fileList) {
      console.log(file);
      console.log(fileList);
      this.imgurl = process.env.VUE_APP_BASE_API + response.data;
      this.flag = true
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(() => {
          this.dialogImageUrl = ''
          done();
        })
        .catch(() => {});
    },
    handleClick(id) {
      deleteSwiper(id).then( () => {
        this.init()
      })
    },
    addSwiper() {
      postSwiper(this.imgurl).then( () => {
        this.init()
        this.dialogImageUrl = ''
      })
    }
  },
  created() {
    this.init()
  }
}
</script>

<style scoped>

</style>